package cn.edu.cdcas.controller;

import cn.edu.cdcas.entity.Answer;
import cn.edu.cdcas.entity.Student;
import cn.edu.cdcas.service.AnswerService;
import cn.edu.cdcas.service.ExamService;
import cn.edu.cdcas.service.QuestionService;
import cn.edu.cdcas.service.StudentService;
import cn.edu.cdcas.utils.Constant;
import cn.edu.cdcas.vo.QuestionNum;
import cn.edu.cdcas.vo.QuestionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class ExamController {
    @Resource
    private StudentService studentService;
    @Resource
    private QuestionService questionService;
    @Resource
    private AnswerService answerService;
//    @Resource
//    private ExamService examService;

    @RequestMapping("exam")
    public String index(HttpSession httpSession, Model model, @RequestParam(required = false) Integer agree) {
        Student student = (Student) httpSession.getAttribute(Constant.USER_IN_SESSION);
        if (student.getExamState() == 0) {
            if (agree != null) {
                student.setExamState(1);//设置初考状态为 正在考试
                student.setExamStart(new Date());
                return "redirect:index";
            }
        }
        if (student.getExamState() == 3 && student.getMakeUpState() == 0) {
            if (agree != null) {
                student.setMakeUpState(1);//设置补考状态为 正在考试
                student.setExamStart(new Date());
                return "redirect:index";
            }
        }

        if (student.getExamState() != 1 && student.getMakeUpState() != 1) {
            return "redirect:index";
        }
        studentService.update(student);
        //此次考试数量分数
        QuestionNum questionNum = (QuestionNum) httpSession.getAttribute("questionNum");
        List<QuestionVo> questions = questionService.getQuestion(student, questionNum);
        model.addAttribute("questionNum", questionNum);
        model.addAttribute("questions", questions);
        return "/exam";
    }

    @RequestMapping("submitTest")
    @ResponseBody
    public Map<String, Object> submitTest(HttpSession httpSession, String answer, Integer answerId) {
        Student student = (Student) httpSession.getAttribute(Constant.USER_IN_SESSION);
        Map<String, Object> map = new HashMap<>();
        if (student.getExamState() != 1 && student.getMakeUpState() != 1) {
            return map;
        }
        //（暂时不改时间）studentService.updateTime(student);
        Answer a = answerService.getById(answerId);
        a.setAnswer(answer);
        QuestionNum question = (QuestionNum) httpSession.getAttribute("questionNum"); //题数分数
        setScoreByQuestionType(question, a);
		/*if (student.getExamState() == 1) {
			student.setExamTime(currentTime);
		} else {
			student.setMakeUpTime(currentTime);
		}*/
        answerService.update(a);
        return map;
    }

    @RequestMapping("submit")
    @ResponseBody
    public Map<String, Object> submit(HttpSession httpSession, String answer, int currentTime, Integer answerId) {
        Student student = (Student) httpSession.getAttribute(Constant.USER_IN_SESSION);
        student.setExamTime(student.getExamTime() / 60);
        Map<String, Object> map = new HashMap<>();
        QuestionNum question = (QuestionNum) httpSession.getAttribute("questionNum"); //题数分数
        if (student.getExamState() != 1 && student.getMakeUpState() != 1) {
            return map;
        }
        if (!StringUtils.isBlank(answer)) {
            Answer a = answerService.getById(answerId);
            a.setAnswer(answer);

            setScoreByQuestionType(question, a);
            answerService.update(a);
        }
		/*if (student.getExamState() == 1) {
			student.setExamTime(currentTime);
		} else {
			student.setMakeUpTime(currentTime);
		}*/
        answerService.complete(student);
        return map;
    }

    private void setScoreByQuestionType(QuestionNum question, Answer a) {
        if (a.getQuestionType() == 1) {
            if (a.getAnswer().equals(a.getResult())) {
                a.setScore(question.getDanxuanScore());
            }
        } else if (a.getQuestionType() == 2) {
            if (a.getAnswer().equals(a.getResult())) {
                a.setScore(question.getDuoxuanScore());
            }
        } else if (a.getQuestionType() == 3) {
            if (a.getAnswer().equals(a.getResult())) {
                a.setScore(question.getPanduanScore());
            }
        }
    }
}
